<?php
require_once '../include/global.php';
require_once '../../convert/include/db_credentials.inc';
//error_reporting (E_ALL);

$pr_id_ = $_POST['pr_id'];
$tel_ = $_POST['tel'];
$d1_ = $_POST['d1'];
$d2_ = $_POST['d2'];
$oPrj1 = array();
$mydbh = DbSimple_Generic::connect('mysql://stat:stat@localhost/call_stat_in');
$mydbh->query('SET NAMES utf8');
$mydbh->setErrorHandler('databaseErrorHandler');
$rows = $mydbh->select('SELECT * FROM projects');
$result = "ok";
foreach ($rows as $numRow => $row) {
    $pr_id = $row['id'];
    $tels = $mydbh->select('SELECT * FROM telephones WHERE pr_id=?d', $pr_id);
    $mytels = array();
    foreach ($tels as $nummtel => $tel) {
		$date1 = new DateTime($tel['date1']);
		$date2_s = $tel['date2'];
		$date2_ = new DateTime($date2_s);
		if ($date2_s=="0000-00-00")
			$date2_ = new DateTime("now");
    	if (preg_match($tel_,$tels['tel']))
		{
			if ($d1_>$date1 and $d1_<$date2)
			{
				echo $row['name'];
				break;
			}
			if ($d2_>$date1 and $d2_<$date2)
			{
				echo $row['name'];
				break;
			}
		}
    }
}
if ($result == "ok")
	echo $result;
?>